package com.ychs.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/12
 */
public class StoreServlet extends HttpServlet {
    /**
     * 苹果库存数量
     */
    private int appleNumber;
    /**
     * 苹果价格
     */
    private float applePrice;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        String appleNumber = config.getInitParameter("appleNumber");
        String applePrice = config.getInitParameter("applePrice");
        if (appleNumber != null && appleNumber.matches("\\d+")) {
            this.appleNumber = Integer.parseInt(appleNumber);
        }
        if (applePrice != null && applePrice.matches("\\d+(\\.\\d+)?")) {
            this.applePrice = Float.parseFloat(applePrice);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //首行增加编码的设置，只是指定了响应流的编码
        //response.setCharacterEncoding("utf-8");

        //指定了响应流的编码，并且指定了浏览器用该编码方式解析数据
        response.setContentType("text/html;charset=utf-8");
        //通过method获取要执行的方法
        String method = request.getParameter("method");
        if (method != null) {
            if (method.equals("sale")) {
                sale(request, response);
            } else if (method.equals("search")) {
                search(request, response);
            } else {
                response.getWriter().println("请输入要执行的业务：sale/search");
            }
        } else {
            //通过response这个响应对应给浏览器返回一些信息
            //response.getWriter().println("请输入要执行的业务：sale/search");
            //session是一个会话，一个会话中可以有多个请求
            //跨请求时用session传递数据
            String msg = "请输入要执行的业务：sale/search";
            request.getSession().setAttribute("msg",msg);
            response.sendRedirect("error.jsp");
        }
    }

    protected void sale(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number = request.getParameter("number");
        if (number == null || !number.matches("\\d+")) {
            //如果没有指定number参数或者格式不是数字
            response.getWriter().println("请指定number为一个整数");
        } else {
            //库存减去售掉的
            if (this.appleNumber >= Integer.parseInt(number)) {
                this.appleNumber -= Integer.parseInt(number);
                //response.getWriter().println("购买成功，共计：" + (Integer.parseInt(number) * this.applePrice));
                //转发到成功页面，并且显示成功的信息
                String msg = "购买成功，共计：" + (Integer.parseInt(number) * this.applePrice);
                request.setAttribute("msg",msg);
                request.getRequestDispatcher("success.jsp").forward(request,response);

            } else {
                response.getWriter().println("最多可以购买" + this.appleNumber + "个");
            }
        }
    }

    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("库存有" + this.appleNumber + "个");
    }
}
